<template>
    <div>
        <el-form style="width: 60%" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="书名" prop="bookName">
                <el-input v-model="ruleForm.bookName"></el-input>
            </el-form-item>
            <el-form-item label="作者" prop="bookCounts">
                <el-input v-model="ruleForm.bookCounts"></el-input>
            </el-form-item>
            <el-form-item label="详情" prop="detail">
                <el-input v-model="ruleForm.detail"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">确认添加</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                ruleForm: {
                    bookName: '',
                    bookCounts: '',
                    detail: '',
                },
                rules: {
                    bookName: [
                        { required: true, message: '请输入书名', trigger: 'blur' },
                        { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ],
                    bookCounts: [
                        { required: true, message: '请输入作者', trigger: 'blur' },
                        { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ],
                    detail: [
                        { required: true, message: '请输入详情', trigger: 'blur' },
                        { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {

            submitForm(formName) {
                const _this = this;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        console.log(_this.ruleForm)
                        axios.post('/book/add',_this.ruleForm).then(function (resp) {
                            console.log(resp);
                            if (resp.data=='success'){
                                _this.$alert('添加成功', '添加成功', {
                                    confirmButtonText: '确定',
                                    callback: action => {
                                        _this.$router.push('/PageOne');
                                    }
                                });

                            }
                        });
                        //alert('submit!');
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>